// pages/cmdView/sleepAid/sleepAid.js

const util = require("../../../utils/util");
const appControlAPI = requirePlugin("hello-plugin")
const alertUtil = require("../../../utils/alertUtil")
const {

  // API的状态码
  succeed,
  failed,

  // 外设模块 cmd参数
  peripheralParamCmd1,
  peripheralParamCmd2,
  // 外设模块 key参数
  deviceId1, // 助眠设备ID
  deviceId2, // 按摩设备ID
  queryDeviceIdKey,
  queryElectricityInformationKey,
  queryDeviceStatusKey,
  queryDeviceGearPositionKey,
  setDeviceOperatingModeKey,
  deviceControlKey,
  peripheraModeControlKey
} = require("../../../utils/constant")

Page({
  timer: null,
  /**
   * 页面的初始数据
   */
  data: {
    canvasText: "接收到设备信息：",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 获取设备ID
   */
  queryDeviceId(){
    appControlAPI.queryDeviceId(queryDeviceIdKey, peripheralParamCmd1, (state, res) => {
      if (state == succeed) {
        this.setData({
          canvasText: this.data.canvasText + "\n返回信息：" + JSON.stringify(res),
          ecgCount: res
        })
      } else {
        this.setData({
          canvasText: this.data.canvasText + "\n返回信息：" + JSON.stringify(res)
        })
      }
      alertUtil.closeToast()
    })
  },

  /**
   * 获取电量信息
   */
  queryElectricityInformation() {
    appControlAPI.queryElectricityInformation(queryElectricityInformationKey, peripheralParamCmd1, (state, res) => {
      if (state == succeed) {
        this.setData({
          canvasText: this.data.canvasText + "\n电量信息：" + JSON.stringify(res),
          ecgCount: res
        })
      } else {
        this.setData({
          canvasText: this.data.canvasText + "\n电量信息：" + JSON.stringify(res)
        })
      }
      alertUtil.closeToast()
    })
  },
  /**
   * 获取设备状态
   */
  queryDeviceStatus() {
    appControlAPI.queryDeviceStatus(queryDeviceStatusKey, peripheralParamCmd2, deviceId1, (state, res) => {
      if (state == succeed) {
        this.setData({
          canvasText: this.data.canvasText + "\n设备状态：" + JSON.stringify(res),
          ecgCount: res
        })
      } else {
        this.setData({
          canvasText: this.data.canvasText + "\n设备状态：" + JSON.stringify(res)
        })
      }
      alertUtil.closeToast()
    })
  },
  /**
   * 设置设备运行模式
   */
  setDeviceOperatingMode() {
    // 参数： key, cmd, 设备ID, 模式, 档位数, 时间（单位秒，时间必须大于等于两分钟）
    appControlAPI.setDeviceOperatingMode(setDeviceOperatingModeKey, peripheralParamCmd1, deviceId1, 3, 12, 200, (state, res) => {
      if (state == succeed) {
        this.setData({
          canvasText: this.data.canvasText + "\n返回信息：" + JSON.stringify(res),
          ecgCount: res
        })
      } else {
        this.setData({
          canvasText: this.data.canvasText + "\n返回信息：" + JSON.stringify(res)
        })
      }
      alertUtil.closeToast()
    })
  },
  /**
   * 获取设备支持档位
   */
  queryDeviceGearPosition() {
    appControlAPI.queryDeviceGearPosition(queryDeviceGearPositionKey, peripheralParamCmd1, deviceId1, (state, res) => {
      if (state == succeed) {
        this.setData({
          canvasText: this.data.canvasText + "\n支持档位信息：" + JSON.stringify(res),
          ecgCount: res
        })
      } else {
        this.setData({
          canvasText: this.data.canvasText + "\n支持档位信息：" + JSON.stringify(res)
        })
      }
      alertUtil.closeToast()
    })
  },
  /**
   * 设备控制
   * @param {设备参数} event 
   */
  deviceControl(event) {
    let action = parseInt(event.currentTarget.dataset.action);
    console.log(action);
    appControlAPI.deviceControl(deviceControlKey, peripheralParamCmd1, deviceId1, action, (state, res) => {
      if (state == succeed) {
        this.setData({
          canvasText: this.data.canvasText + "\n返回信息：" + JSON.stringify(res),
          ecgCount: res
        })
      } else {
        this.setData({
          canvasText: this.data.canvasText + "\n返回信息：" + JSON.stringify(res)
        })
      }
      alertUtil.closeToast()
    })
  },
  /**
   * 关机/重启
   * @param {设备参数} event 
   */
  peripheraModeControl(event) {
    let param = parseInt(event.currentTarget.dataset.action);
    appControlAPI.peripheraModeControl(peripheraModeControlKey, peripheralParamCmd2, param, (state, res) => {
      if (state == succeed) {
        this.setData({
          canvasText: this.data.canvasText + "\n返回信息：" + JSON.stringify(res),
          ecgCount: res
        })
      } else {
        this.setData({
          canvasText: this.data.canvasText + "\n返回信息：" + JSON.stringify(res)
        })
      }
      alertUtil.closeToast()
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})